import { createStore } from "vuex";

import { setLocal, getLocal, removeLocal } from "@/assets/js/funcs";
import user from "./modules/user";
import resume from "./modules/resume";

/** 创建并导出Vuex store对象 */
const store = createStore({
    state: {
        gdepUserInfo: getLocal("gdepUserInfo") ?? {},
    },
    getters: {
        // 判断用户是否已登录 - 通过检查token和是否有账号信息
        isLoggedIn: (state) => {
            return (
                state.gdepUserInfo &&
                state.gdepUserInfo.token &&
                state.gdepUserInfo.account &&
                Object.keys(state.gdepUserInfo).length > 0
            );
        },
        // 获取用户类型 - 返回"user"或"company"
        userType: (state) => {
            return state.gdepUserInfo?.type || "";
        },
        // 获取用户名 - 优先返回user_name，没有则返回account
        username: (state) => {
            return state.gdepUserInfo?.user_name || state.gdepUserInfo?.account || "";
        },
        // 获取用户头像
        userAvatar: (state) => {
            return state.gdepUserInfo?.avatar || "";
        },
    },
    mutations: {
        setUserInfo(state, provider) {
            state.gdepUserInfo = provider;
            setLocal("gdepUserInfo", provider);
        },
        setLogout(state) {
            state.gdepUserInfo = {};
            removeLocal("gdepUserInfo");
        },
    },
    actions: {},
    modules: {
        user,
        resume,
    },
});

export default store;
